package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.Map;

import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.modules.sys.controller.AbstractController;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.service.ShopService;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;



/**
 *
 *
 * @author jeson
 * @email 574197743@qq.com
 * @date 2021-03-23 20:03:34
 */
@RestController
@RequestMapping("business/shop")
public class ShopController extends AbstractController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:shop:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity user = this.getUser();
        params.put("business_id",user.getBusinessId());
        PageUtils page = shopService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("business:shop:info")
    public R info(@PathVariable("id") Long id){
		ShopEntity shop = shopService.getById(id);

        return R.ok().put("shop", shop);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("business:shop:save")
    public R save(@RequestBody ShopEntity shop){
        SysUserEntity sysUser = ShiroUtils.getUserEntity();
        if(sysUser!=null){
            shop.setBusinessId(sysUser.getBusinessId().intValue());
        }
		shopService.save(shop);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("business:shop:update")
    public R update(@RequestBody ShopEntity shop){
		shopService.updateById(shop);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("business:shop:delete")
    public R delete(@RequestBody Long[] ids){
		shopService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 财务明细
     */
    @RequestMapping("/financialDetails")
    public R financialDetails(@RequestParam Map<String, Object> params) {
        //1.机构帐号查询旗下所有分店   2.店长
        SysUserEntity userEntity = this.getUser();
        params.put("businessId", userEntity.getBusinessId());
        if(userEntity.getType() == 1){

        }else if(userEntity.getType() == 2){
            params.put("shopId", this.getTeacherShopId());
        }
        //1.按照分销查询汇总2.按照店、人维度汇总3.按店、人维度明细
        PageUtils page = null;
        String flag = (String) params.get("flag");
        if("1".equals(flag)){
            page = shopService.financeByBistribution(params);
        }else if("2".equals(flag)){
            page = shopService.financeByShopAndTeacher(params);
        }else if("3".equals(flag)){
            page = shopService.financeDetailedByShopAndTeacher(params);
        }
        return R.ok().put("page", page);
    }

}
